/*
 * DXObject.cpp
 *
 *  Created on: Oct 28, 2012
 *      Author: Bhupendra Aole
 */

#include "DXObject.h"
#include <d3dx11.h>
#include <D3dcompiler.h>
#include <sstream>

using namespace std;

namespace aole {

int DXObject::setProperty( const char* name, const char* value ) {
	istringstream ss( value );

	if (!strcmp(name, "r")){
		ss >> color.x;
		return 1;
	} else if (!strcmp(name, "g")){
		ss >> color.y;
		return 1;
	} else if (!strcmp(name, "b")){
		ss >> color.z;
		return 1;
	} else if (!strcmp(name, "a")){
		ss >> color.w;
		return 1;
	}

	return 0;
}

//--------------------------------------------------------------------------------------
// Helper for compiling shaders with D3DX11
//--------------------------------------------------------------------------------------
HRESULT DXObject::CompileShaderFromFile( WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut )
{
    HRESULT hr = S_OK;

    DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
    // Setting this flag improves the shader debugging experience, but still allows
    // the shaders to be optimized and to run exactly the way they will run in
    // the release configuration of this program.
    dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif

    ID3DBlob* pErrorBlob;
    hr = D3DX11CompileFromFile( szFileName, NULL, NULL, szEntryPoint, szShaderModel,
        dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL );
    if( FAILED(hr) )
    {
        if( pErrorBlob != NULL )
            OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
        if( pErrorBlob ) pErrorBlob->Release();
        return hr;
    }
    if( pErrorBlob ) pErrorBlob->Release();

    return S_OK;
}

}

